package sanphone.com.runningschool.RequestHttp;


import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.util.Log;

import org.json.JSONObject;

import java.io.IOException;


import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RequestHttp {
    SharedPreferences sprf;
    private String url;//接口地址
    private OkHttpClient mclient;
    private Context mContext;
    String head;



    public RequestHttp(String url,Context context) {
        this.url = url;
       mContext=context;
    }
    public void getPreferences(){
        ContextWrapper wrapper=new ContextWrapper(mContext);
        sprf=wrapper.getSharedPreferences("data",Context.MODE_PRIVATE);
        head=sprf.getString("token",null);
        Log.e("token", head );
    }

    public String post(final JSONObject json) throws IOException {
        /**okhttp框架**/
        getPreferences();
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        mclient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
        Request request = new Request.Builder()
                .url(url)
                .addHeader("token",head)
                .post(requestBody)
                .build();
        Response response = mclient.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("发生未知错误" + response);
        }
    }

    public String get() throws IOException {
        /**okhttp框架**/
        getPreferences();
            mclient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .addHeader("token",head)
                        .build();
                    Response response = mclient.newCall(request).execute();
                    if (response.isSuccessful()) {
                      return response.body().string();
                    }else {
                        throw new IOException("发生未知错误" + response);
                    }
            }

            public String login( JSONObject json) throws IOException {

                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                mclient = new OkHttpClient();
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(json));
                Request request = new Request.Builder()
                        .url(url)
                        .post(requestBody)
                        .build();
                Response response = mclient.newCall(request).execute();
                if (response.isSuccessful()) {
                    return response.body().string();
                } else {
                    throw new IOException("发生未知错误" + response);
                }
            }


    }

